# encoding: utf-8

module ValidateSmsCode
  module InstanceMethods
    def validate_sms_code
      if require_sms_code && ( sms_code.blank? || (sms_code != valid_sms_code) || (valid_sms_code.nil?))
        self.errors.add(:sms_code, "手机验证码错误")
        return false
      end
    end
  end

  def self.included(receiver)
    receiver.class_eval do
      attr_accessor :require_sms_code, :valid_sms_code, :sms_code
      validate :validate_sms_code, on: :create
    end

    receiver.send :include, InstanceMethods
  end

end
